//
// Created by tao on 2023/1/4.
//

#ifndef LAB_EXAMPLEBINDER_H
#define LAB_EXAMPLEBINDER_H


class IExampleService:public IInterface{
public:
        DECLARE_META_INTERFACE(ExampleService);
        virtual int get()=0;
        virtual void set(int value)=0;

}

class BnExampleService:public BnInterface<IExampleService>{
public:
        virtual status_t onTransact(uint32_t code,const Parcel& data,
                         Parcel * reply ,uint32_t flags=0);
};

enum{
    EXAMPLE_GET=IBinder::FIRST_CALL_TRANSACTION,
    EXAMPLE_SET
};

class BpExampleService:public BpInterface<IExampleService>{

public:
        BpExampleService(const sp<Binder> &impl):BpInterface<IExampleService>(impl){}
        virtual int get(){
        Parcel data,reply;
        data.writeInterfaceToken(IExampleService::getInterfaceDescriptor());
        remote()->transact(EXAMPLE_GET,data,&reply);
        return reply.readInt();
        }
        virtual void set(int value){

        }
}

IMPLEMENT_META_INTERFACE(ExampleService,"ExampleDescriptor");

status_t BnExampleService::onTransact(
        uint32_t code,const Parcel& data, Parcel * reply ,uint32_t flags=0){
        switch(code){
        case EXAMPLE_GET:
        CHECK_INTERFACE(IExampleService,data,reply);
        reply->writeInt(get());
        break;
        case EXAMPLE_SET:
        CHECK_INTERFACE(IExampleService,data,reply);
        set(data->readInt());
        break;
        default:
        return BBbinder::onTransact(code,data,reply,flags);
        }
        return NO_ERROR;
}





#endif //LAB_EXAMPLEBINDER_H
